package com.sh.service.impl;

import com.sh.mapper.UserMapper;
import com.sh.pojo.User;
import com.sh.service.UserService;
import com.sh.utils.Md5Util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {
    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUserName(String username) {
        User u = userMapper.findByUserName(username);
        return u;
    }

    @Override
    public void register(String username, String password) {
        //加密
        String md5String = Md5Util.getMD5String(password);
        User u=new User();
        u.setPassword(md5String);
        u.setUsername(username);
        u.setCreateTime(LocalDateTime.now());
        u.setUpdateTime(LocalDateTime.now());
        //添加
        userMapper.add(u);
    }

    @Override
    public boolean login(String username, String password) {
        User u=new User();
        u.setUsername(username);
        u.setPassword(password);
        if(userMapper.login(u)!=0)return true;
        return false;
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateavtar(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateavtar(user);
    }

    @Override
    public void updatePwd(Integer id, String newPwd) {
        User u=new User();
        u.setId(id);
        u.setPassword(newPwd);
        userMapper.updatePwd(u);
    }
}
